package nkw;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import robocode.control.testing.RobotTestBed;
import robocode.BattleResults;
import robocode.control.events.BattleCompletedEvent;

/**
 * Verifies that CompetitiveRobot beats Crazy at least 70% of the time.
 * 
 * @author Noah Woodden
 */
public class TestVersusCrazy extends RobotTestBed {

  /**
   * Specifies that Crazy and CompetitiveRobot are to be matched up in this test case.
   * 
   * @return The comma-delimited list of robots in this match.
   */
  @Override
  public String getRobotNames() {
    return "sample.Crazy,nkw.CompetitiveRobot";
  }

  /**
   * This test runs for 10 rounds.
   * 
   * @return The number of rounds.
   */
  @Override
  public int getNumRounds() {
    return 10;
  }

  /**
   * The actual test, which asserts that CompetitiveRobot has won 70% of rounds against Crazy.
   * 
   * @param event Details about the completed battle.
   */
  @Override
  public void onBattleCompleted(BattleCompletedEvent event) {
    // Return the results in order of getRobotNames.
    BattleResults[] battleResults = event.getIndexedResults();
    // Sanity check that results[1] is CompetitiveRobot (not strictly necessary, but illustrative).
    BattleResults botResults = battleResults[1];
    String robotName = botResults.getTeamLeaderName();
    assertEquals("Check that results[1] is CompetitiveRobot", robotName, "nkw.CompetitiveRobot*");

    double winPerc = (double)botResults.getFirsts() / (double)getNumRounds();
    if (winPerc >= .7) {
      System.out.println(botResults.getFirsts());
      System.out.println("CompetitiveRobot maintained required winning percentage at " + winPerc
          + "%");
    }
    else {
      assertFalse("CompetitiveRobot did not maintain required winning percentage at " + winPerc
          + "%", true);
    }
  }

  /**
   * Returns true if the battle should be deterministic and thus robots will always start in the
   * same position each time.
   * 
   * Override to return false to support random initialization.
   * 
   * @return True if the battle will be deterministic.
   */
  @Override
  public boolean isDeterministic() {
    return false;
  }
}
